import { Processor, IProcessor } from '@midwayjs/bull';
import axios from 'axios';
import Privacy from '../Privacy';
import { Application } from '@midwayjs/koa';
import { Inject } from '@midwayjs/core';
import { RedisService } from '@midwayjs/redis';

import { App } from '@midwayjs/decorator';
@Processor('test', {
    // 重复执行下面的任务多少次 
  repeat: {

    // cron 决定 有一定的语法
    cron: "0 */2 * * *"
    // cron: FORMAT.CRONTAD
  }
})
export class TestProcessor implements IProcessor {

  @App()
  app: Application;

  @Inject()
  redisService: RedisService;
  // 获取小程序access_token
  async execute() {
        console.log("执行了");

    this.app.getEnv()
    console.log("环境", this.app.getEnv());

    if( this.app.getEnv() == 'local'){
      return 
    }

       let {data} =  await axios.get("https://api.weixin.qq.com/cgi-bin/token",{
           params:{
            grant_type:"client_credential",
            appid:Privacy.appid,
            secret:Privacy.AppSecret
          }
        })   
        console.log(data.access_token);
        await this.redisService.set("access_token",data.access_token,"EX",7200)
          console.log("token设置成功");
          
          
  }
}